function checkuserlogin(url,redirecturl)
{
    $$.ajax({
        contentType:'OPTIONS',
        crossDomain:true,
        method:'GET',
        async:false,
        url:url,
        data:{},
        dataType:'json',
        xhrFields:{"withCredentials":true},
        success:function (res) {
            //console.log('用户登陆',res);
            if (res.code == 0){
                window.location.href = redirecturl;
            } else {
                GlobalUserid = parseInt(res.data.uid);
                GlobalOpenid = res.data.wx_openid;
                GlobalFirst = res.data.isfirst;
                // Globaltoken = res.data.token;
                //console.log(GlobalUserid,GlobalOpenid);
            }
        }
    });
}
function showNfirst(isfirst){
    if (isfirst==0){
        var html='<div class="popup" style="background: transparent;padding-bottom: 40px;">'+
            '<div class="flex-column flex-justify-center flex-align-center" style="width: 100%;height: 100%;">'+
            '<img src="http://wap.nldou.com/comm/img/notify.png" style="width: 70%;">'+
            '<div style="margin-top: 20px;">'+
            '<i class="f7-icons color-green close-popup" style="font-size: 40px;width: 40px;height: 40px;">'+
            'close_round_fill</i></div>'+
            '</div>'+
            '</div>';
        Nldouapp.popup(html);
    }
}
// function nativepay(num){
//     $$.ajax({
//         contentType:'OPTIONS',
//         crossDomain:true,
//         url:Nativepay_url,
//         method:"POST",
//         data: {num:num,uid:GlobalUserid},
//         dataType:'json',
//         success: function (res) {
//             //console.log('扫码统一下单',res);
//             //alert(JSON.stringify(res));
//             if(res.code==1){
//                 chargestatus=false;
//                 mainView.router.load({url:'nativepay.html',context:res.data});
//             }else{
//                 //console.log(res.msg);
//                 Nldouapp.alert('下单失败,请重试','');
//                 chargestatus=false;
//             }
//         },
//         error:function (res) {
//             alert(JSON.stringify(res));
//             Nldouapp.alert('下单失败,请重试','');
//             chargestatus=false;
//         }
//     });
// }
function crosspay(num)
{
    var fee = 0;
    switch (parseInt(num)){
        case 50:
            fee = "0.5";
            break;
        case 100:
            fee = "1";
            break;
        case 500:
            fee = "4.98";
            break;
        case 1000:
            fee = "9.95";
            break;
        case 5000:
            fee = "49.5";
            break;
        case 10000:
            fee = "98.5";
            break;
        case 20000:
            fee = "196.5";
            break;
        case 50000:
            fee = "490";
            break;
        default:
            return false;
    }
    mainView.router.load({url:'nativepay.html',context:{num:num,fee:fee}});
}
function pay(num) {
    $$.ajax({
        contentType:'OPTIONS',
        crossDomain:true,
        url:Prepay_url,
        method:"POST",
        data: {num:num,uid:GlobalUserid,openid:GlobalOpenid},
        dataType:'json',
        success: function (res) {
            //console.log('统一下单',res);
            if(res.code==1){
                wx.chooseWXPay({
                    timestamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,
                    success: function (res) {
                        //console.log('支付成功',res);
                        Nldouapp.alert('您已成功充值'+num+'个能量豆','');
                        if($$('#Ucenter .beansnum').length > 0) {
                            var beans = parseInt($$('#Ucenter .beansnum').data('beans'));
                            var add = parseInt(num);
                            var total = beans+add;
                            $$('#Ucenter .beansnum').data('beans',total);
                            $$('#Ucenter .beansnum').html(total);
                        }
                        chargestatus=false;
                    },
                    fail:function(){
                        chargestatus=false;
                        crosspay(num);
                    },
                    cancel:function () {
                        chargestatus=false;
                    }
                });
            }else{
                //console.log(res.msg);
                Nldouapp.alert('下单失败,请重试','');
                chargestatus=false;
            }
        },
        error:function () {
            Nldouapp.alert('下单失败,请重试','');
            chargestatus=false;
        }
    });
}
function sharecallback(url,sharetype)
{
    request(Sharecallback_url,'POST',{uid:GlobalUserid,url:url,type:sharetype},function (res) {
        if (res.code==1&&res.data.firstshare){
            var html = '<div class="flex-row flex-nowrap flex-justify-center flex-align-center">' +
                '<i class="f7-icons color-green bold" style="margin-right: 15px;width: 25px;height: 25px;">alarm</i>' +
                '<span class="pubinfo bold">首次分享成功,获得100能量豆</span>' +
                '</div>';
            Nldouapp.modal({text:html});
            setTimeout(function () {
                Nldouapp.closeModal('.modal');
                $$('.modal').remove();
                $$('.modal-overlay-visible').remove();
            },2000);
        }
    });
}
function setShare(title,url,desc){
    //console.log('setshare:'+title+';'+desc+';'+url);
    wx.onMenuShareTimeline({
        title: title,
        link: url,
        imgUrl: Shareimg_url,
        success: function () {
            sharecallback(url,'timeline');
            return true;
        }
    });
    wx.onMenuShareAppMessage({
        title: title,
        desc: desc,
        link: url,
        imgUrl: Shareimg_url,
        type: 'link',
        success: function () {
            sharecallback(url,'appmsg');
            return true;
        }
    });
}
function getShareContent() {
    var curpage = $$('.view-main').data('page');
    var share = {};
    switch (curpage){
        case 'Subject':
            share.title = $$('#Subject').data('sharetitle');
            share.desc = $$('#Subject').data('sharedesc');
            var sid = $$('#Subject').data('id');
            share.url = Share_url+'&page=subject'+'&param=sid.'+sid;
            break;
        case 'Question':
        case 'Beansrank':
        case 'Dechargebeans':
            share.title = $$('#Question').data('sharetitle');
            share.desc = $$('#Question').data('sharedesc');
            var qid = $$('#Question').data('id');
            share.url = Share_url+'&page=question'+'&param=qid.'+qid;
            break;
        case 'Answer':
            share.title = $$('#Answer').data('sharetitle');
            share.desc = $$('#Question').data('sharedesc');
            var aid = $$('#Answer').data('id');
            share.url = Share_url+'&page=answer'+'&param=aid.'+aid;
            break;
        case 'Questionrank':
            share.title = '一问奕答女神榜';
            share.desc = '您的朋友邀请您，为他困扰的问题贡献一份力哦~~';
            share.url = Share_url+'&page=questionrank';
            break;
        default:
            share.title = '一问奕答';
            share.desc = '复旦大学社会学教授沈奕斐，为您解答家庭教育的各类问题，陪您共走育儿路';
            share.url = Share_url;
    }
    return share;
}
function sofa(length,container) {
    container.find('.sofa').remove();
    if(length == 0) {
        var html = Nldouapp.SofaTemplate();
        container.append(html);
    }
}
function contentempty(length,content,container) {
    container.find('.empty').remove();
    if(length == 0) {
        var html = Nldouapp.EmptyTemplate({content:content});
        container.append(html);
    }
}
function request(url,method,data,success){
    // data.uid = GlobalUserid;
    // data.token = Globaltoken;
    $$.ajax({
        method:method,
        url:url,
        data:data,
        dataType:'json',
        success:success
    });
}
function prequest(url,method,data,success,error){
    // data.uid = GlobalUserid;
    // data.token = Globaltoken;
    $$.ajax({
        contentType:'OPTIONS',
        crossDomain:true,
        method:method,
        url:url,
        data:data,
        timeout:5000,
        dataType:'json',
        success:success,
        error:error
    });
}
/******个人中心******/
function getUcInfo(url)
{
    request(url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('个人中心信息',url,res);
        if (res.code == 1){
            var html = Nldouapp.UcHeaderTemplate(res.data);
            $$('#Ucenter .ucheader').html(html);
            // $$('.ucnav .center').html(res.data.userName);
        }
    });
}
function signComplete(beans)
{
    var html = '<div class="flex-row flex-nowrap flex-justify-center flex-align-center">' +
        '<i class="f7-icons color-green bold" style="margin-right: 15px;width: 25px;height: 25px;">alarm</i>' +
        '<span class="pubinfo bold">签到成功,获得10颗能量豆</span>' +
        '</div>';
    Nldouapp.modal({text:html});
    setTimeout(function () {
        Nldouapp.closeModal();
        $$('#Ucenter .beansnum').html(beans);
        var signbtn = $$('#Ucenter .signbtn');
        signbtn.removeClass('bg-green');
        signbtn.removeClass('signbtn');
        signbtn.addClass('bg-gray');
        signbtn.html('今日已签');
    },2000);
}
function signToday(url)
{
    prequest(url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('个人中心签到',url,res);
        if (res.code == 1){
            if (res.data.beans == 0){
                Nldouapp.alert('今日已经签到了');
            }else{
                signComplete(res.data.beans);
            }
        }
    },function () {
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function uploadimage(mediaid)
{
    request(Uploadimg_url,'POST',{uid:GlobalUserid,mediaid:mediaid},function (res){
        //alert(JSON.stringify(res));
    })
}
function changeicon(localid)
{
    //生成预览
    wx.getLocalImgData({
        localId: localid,
        success: function (res) {
            var localData = res.localData;
            $$('#Ucenter .editiconbtn').attr('src',localData);
        }
    });
    //上传到服务器
    wx.uploadImage({
        localId: localid,
        isShowProgressTips: 1,
        success: function (res) {
            uploadimage(res.serverId);
        }
    });
}
function getCity(cityselected,areaselected)
{
    request(Getcity_url,'GET',{},function (res) {
        //console.log('获取城市列表',res);
        if (res.code == 1){
            var html = Nldouapp.CitylistTemplate(res.data);
            $$('select[name="livecity"]').html(html);
            if (cityselected) {
                $$('select[name="livecity"]').val(cityselected);
                var cityname = $$('select[name="livecity"]').find('option[value="'+cityselected+'"]').html();
                $$('select[name="livecity"]').parent().find('.item-after').html(cityname);
                getArea(cityselected,areaselected);
            }
        }
    });
}
function getArea(cityselected,areaselected)
{
    request(Getarea_url,'POST',{cityid:cityselected},function (res) {
        //console.log('获取区域列表',res);
        if (res.code == 1){
            var html = Nldouapp.ArealistTemplate(res.data);
            $$('select[name="livearea"]').html(html);
            if (areaselected) {
                $$('select[name="livearea"]').val(areaselected);
                var areaname = $$('select[name="livearea"]').find('option[value="'+areaselected+'"]').html();
                $$('select[name="livearea"]').parent().find('.item-after').html(areaname);
            }
        }
    });
}
function getProfile()
{
    request(Getprofile_url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('个人信息列表',res);
        var phtml = Nldouapp.ProfilelistTemplate(res.data.user);
        $$('form#Profileform').html(phtml);
        Nldouapp.calendar({
            input:'#bcal',
            monthNames:['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
            weekHeader:false,
            closeOnSelect:true
        });
        getCity(res.data.user.livecity,res.data.user.livearea);
        $$('select[name="livecity"]').on('change',function () {
            var cityid = $$(this).val();
            getArea(cityid);
            $$('select[name="livearea"]').parent().find('.item-after').html('');
        });
        var chtml = Nldouapp.ChilditemTemplate(res.data.child);
        $$('#Profile .child-wrap').append(chtml);
    });
}
function checkProfile() {
    var data = Nldouapp.formToData('#Profileform');
    var userName = data['userName'].replace(/\s/g,"");
    var wc = userName.length;
    if (wc == 0) {Nldouapp.alert('请输入姓名','');profileuploading = false;return false;}

    var userGender = data['userGender'];
    wc = userGender.length;
    if (wc == 0) {Nldouapp.alert('请选择性别','');profileuploading = false;return false;}

    var telNum = data['telNum'];
    if(!(/^1\d{10}$/).test(telNum)){Nldouapp.alert('请输入正确的11位手机号','');profileuploading = false;return false;}

    var wx_num = data['wx_num'].replace(/^\s|\s$/,"");

    var email = data['email'].replace(/\s/g,"");
    wc = email.length;
    if (wc>0 && email.indexOf('@') == -1) {Nldouapp.alert('请输入正确的邮箱','');profileuploading = false;return false;}

    var birthDay = data['birthDay'];
    var eduLevel = data['eduLevel'];
    var livecity = data['livecity'];
    var livearea = data['livearea'];

    var child = [];
    $$('.child-wrap .childitem').each(function () {
        var id = $$(this).data('id');
        var cg = $$(this).data('gender');
        var cs = $$(this).data('school');
        var cb = $$(this).data('birth');
        child.push({'id':id,'gender':cg,'birthDay':cb,'school':cs});
    });

    var result = {'uid':GlobalUserid,'userName':userName,'userGender':userGender,'telNum':telNum,'wx_num':wx_num,'email':email
        ,'birthDay':birthDay,'eduLevel':eduLevel,'livecity':livecity,'livearea':livearea,'child':child,'childdelete':childdelte};

    return result;
}
function subprofile(data)
{
    startupload();
    request(Subprofile_url,'POST',data,function (res) {
        //console.log('上传个人信息',res);
        if (res.code == 1){
            completeupload();
            if(res.data.firstprofile){
                firstupload('首次提交信息,获得100能量豆');
            }
            profileuploading=false;
            mainView.router.back();
        }else{
            failupload();
            profileuploading=false;
        }
    });
}
function getMyfollowq(url,page,append)
{
    if (!append) {
        $$('#Myfollows .followlist').find('a').remove();
        $$('#Myfollows .infinite-scroll').find('.infinite-scroll-preloader').remove();
        var modal = '<div class="infinite-scroll-preloader">'+
            '<div class="preloader"></div>'+
            '</div>';
        $$('#Myfollows .infinite-scroll').append(modal);

    }
    request(url,'POST',{uid:GlobalUserid,pagesize:myfollowspagesize,page:page},function (res) {
        //console.log('个人中心我的关注问题',res);
        if (res.code == 1){
            var html = Nldouapp.QitemTemplate(res.data.question);
            if (!append){
                Nldouapp.attachInfiniteScroll('#Myfollows .infinite-scroll');
                contentempty(res.data.question.length,'还没有关注的内容~~',$$('#Myfollows .followlist'));
            }
            $$('#Myfollows .followlist').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Myfollows .infinite-scroll'));
            $$('#Myfollows .infinite-scroll-preloader').remove();
        }
        contentempty($$("#Myfollows .followlist").find('a').length,'还没有关注的内容~~',$$("#Myfollows .followlist"));
        myfollowsloading = false;
    });

}
function getMyfollowsub(url,page,append)
{
    if (!append) {
        $$('#Myfollows .followlist').find('a').remove();
        $$('#Myfollows .infinite-scroll').find('.infinite-scroll-preloader').remove();
        var modal = '<div class="infinite-scroll-preloader">'+
            '<div class="preloader"></div>'+
            '</div>';
        $$('#Myfollows .infinite-scroll').append(modal);
    }
    request(url,'POST',{uid:GlobalUserid,pagesize:myfollowspagesize,page:page},function (res) {
        //console.log('个人中心我的关注话题',res);
        if (res.code == 1){
            var html = Nldouapp.SubitemTemplate(res.data.subject);
            if (!append){
                Nldouapp.attachInfiniteScroll('#Myfollows .infinite-scroll');
                contentempty(res.data.subject.length,'还没有关注的内容~~',$$('#Myfollows .followlist'));
            }
            $$('#Myfollows .followlist').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Myfollows .infinite-scroll'));
            $$('#Myfollows .infinite-scroll-preloader').remove();
        }
        contentempty($$("#Myfollows .followlist").find('a').length,'还没有关注的内容~~',$$("#Myfollows .followlist"));
        myfollowsloading = false;
    });
}
function getMycollect(url,page,append)
{
    request(url,'POST',{uid:GlobalUserid,pagesize:mycollectspagesize,page:page},function (res) {
        //console.log('个人中心我的收藏',res);
        if (res.code == 1){
            var html = Nldouapp.CollectitemTemplate(res.data.answer);
            if (!append) {
                $$('#Mycollects .collectlist').find('.aitem').remove();
                $$('#Mycollects .infinite-scroll').find('.infinite-scroll-preloader').remove();
                var modal = '<div class="infinite-scroll-preloader">'+
                    '<div class="preloader"></div>'+
                    '</div>';
                $$('#Mycollects .infinite-scroll').append(modal);
                Nldouapp.attachInfiniteScroll('#Mycollects .infinite-scroll');
                contentempty(res.data.answer.length,'还没有收藏的回答~~',$$('#Mycollects .collectlist'));
            }
            $$('#Mycollects .collectlist').append(html);

        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Mycollects .infinite-scroll'));
            $$('#Mycollects .infinite-scroll-preloader').remove();
        }
        contentempty($$("#Mycollects .collectlist").find('a').length,'还没有收藏的回答~~',$$("#Mycollects .collectlist"));
        mycollectsloading = false;
    });
}
function getMyanswer(url,page,append)
{
    request(url,'POST',{uid:GlobalUserid,pagesize:myanswerpagesize,page:page},function (res) {
        //console.log('个人中心我的回答',url,res);
        if (res.code == 1){
            var html = Nldouapp.MyansweritemTemplate(res.data.answer);
            if (!append) {
                $$('#Myanswers .myanswerlist').find('.aitem').remove();
                $$('#Myanswers .infinite-scroll').find('.infinite-scroll-preloader').remove();
                var modal = '<div class="infinite-scroll-preloader">'+
                    '<div class="preloader"></div>'+
                    '</div>';
                $$('#Myanswers .infinite-scroll').append(modal);
                Nldouapp.attachInfiniteScroll('#Myanswers .infinite-scroll');
                contentempty(res.data.answer.length,'还没有回答过问题~~',$$('#Myanswers .myanswerlist'));
            }
            $$('#Myanswers .myanswerlist').append(html);

        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Myanswers .infinite-scroll'));
            $$('#Myanswers .infinite-scroll-preloader').remove();
        }
        contentempty($$("#Myanswers .myanswerlist .aitem").length,'还没有回答过问题~~',$$("#Myanswers .myanswerlist"));
        myanswerloading = false;
    });
}
function getMyquestion(url,page,append)
{
    request(url,'POST',{uid:GlobalUserid,pagesize:myquestionpagesize,page:page},function (res) {
        //console.log('个人中心我的提问',url,res);
        if (res.code == 1){
            var html = Nldouapp.MyquestionitemTemplate(res.data.question);
            if (!append) {
                $$('#Myquestions .myqlist').find('.qitem').remove();
                $$('#Myquestions .infinite-scroll').find('.infinite-scroll-preloader').remove();
                var modal = '<div class="infinite-scroll-preloader">'+
                    '<div class="preloader"></div>'+
                    '</div>';
                $$('#Myquestions .infinite-scroll').append(modal);
                Nldouapp.attachInfiniteScroll('#Myquestions .infinite-scroll');
                contentempty(res.data.question.length,'还没有提问~~',$$('#Myquestions .myqlist'))
            }
            $$('#Myquestions .myqlist').append(html);

        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Myquestions .infinite-scroll'));
            $$('#Myquestions .infinite-scroll-preloader').remove();
        }
        contentempty($$("#Myquestions .myqlist .qitem").length,'还没有提问~~',$$("#Myquestions .myqlist"));
        myquestionloading = false;
    });
}
// function checkcharge(num) {
//     num = num.replace(/\s/g,"");
//     if(!(/^[1-9][0-9]*$/).test(num)){
//         Nldouapp.alert('请输入正确的金额');
//         return false;
//     }
//     return parseInt(num);
// }
/******发现页******/
function getDiscoverData(url)
{
    request(url,'POST',{},function (res) {
        //console.log('发现页信息',url,res);
        if (res.code == 1){
            var banner = res.data.banner;
            var subject = res.data.subject;
            var bannerhtml = Nldouapp.SwiperslideTemplate(banner);
            var subhtml = Nldouapp.SwiperslideRectTemplate(subject);

            $$('#Discover .swiper-discover .swiper-wrapper').html(bannerhtml);
            $$('#Discover .swiper-subject .swiper-wrapper').html(subhtml);

            Nldouapp.swiper('.swiper-discover',{
                autoplay:4000,
                // loop:true,
                pagination:'.swiper-pagination-discover'
            });
            Nldouapp.swiper('.swiper-subject',{
                slidesPerView:'auto',
                spaceBetween:10,
                freeMode:true
            });
        }
    });
}
function getQuestion(page,url,append)
{
    if (!append) {
        $$('#Discover .qlist').find('.qitem').remove();
        $$('#Discover .infinite-scroll').find('.infinite-scroll-preloader').remove();
        var modal = '<div class="infinite-scroll-preloader">'+
            '<div class="preloader"></div>'+
            '</div>';
        $$('#Discover .infinite-scroll').append(modal);
        Nldouapp.attachInfiniteScroll('#Discover .infinite-scroll');
    }
    request(url,'POST',{page:page,pagesize:discoverpagesize},function (res) {
        //console.log('发现页问题列表',res);
        setTimeout(function () {
            if (res.code == 1){
                var qhtml = Nldouapp.QitemTemplate(res.data.question);
                $$('#Discover .qlist').append(qhtml);
            }
            if (res.data.end == 1){
                Nldouapp.detachInfiniteScroll($$('#Discover .infinite-scroll'));
                $$('#Discover .infinite-scroll-preloader').remove();
            }
            discoverloading = false;
        },1000);
    });
}
function getNotifyCount(url)
{
    request(url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('消息icon',res);
        if (res.code==1){
            Sysmsgnum = parseInt(res.data.sys);
            Usermsgnum = parseInt(res.data.user);
            var html = Nldouapp.NotifyiconTemplate({unread:res.data.unread});
            $$('.toolbar .notifyicon').remove();
            $$('.toolbar .notifybtn').append(html);
        }
    });
}
function readNotify(url,msgid,msgtype)
{
    request(url,'POST',{uid:GlobalUserid,msgid:msgid,msgtype:msgtype},function () {
        //console.log('阅读消息',res);
    });
}
function clearNotify(url)
{
    request(url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('清空消息',res);
        $$('#Notify .notifylistwrap').find('li').remove();
    });
}
function getUserNotify(url,page,append)
{
    $$('#Notify .toolbar').show();
    if (!append){
        $$('#Notify .notifylistwrap').find('li').remove();
        $$('#Notify .infinite-scroll').find('.infinite-scroll-preloader').remove();
        var modal = '<div class="infinite-scroll-preloader">'+
            '<div class="preloader"></div>'+
            '</div>';
        $$('#Notify .infinite-scroll').append(modal);
    }
    request(url,'POST',{uid:GlobalUserid,page:page,pagesize:notifypagesize},function (res) {
        //console.log('消息列表',res);
        if (res.code == 1){
            var html = Nldouapp.NotifylistTemplate(res.data);
            if (!append){
                Nldouapp.attachInfiniteScroll('#Notify .infinite-scroll');
                contentempty(res.data.notify.length,'还没有消息~~',$$('#Notify .notifylistwrap'));
            }
            $$('#Notify .notifylistwrap').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Notify .infinite-scroll'));
            $$('#Notify .infinite-scroll-preloader').remove();
        }
        contentempty($$('#Notify .notifylistwrap').find('li').length,'还没有消息~~',$$('#Notify .notifylistwrap'));
        notifyloading = false;
    });
}
function getSysNotify(url,page,append)
{
    $$('#Notify .toolbar').hide();
    if (!append){
        $$('#Notify .notifylistwrap').find('li').remove();
        $$('#Notify .infinite-scroll').find('.infinite-scroll-preloader').remove();
        var modal = '<div class="infinite-scroll-preloader">'+
            '<div class="preloader"></div>'+
            '</div>';
        $$('#Notify .infinite-scroll').append(modal);
    }
    request(url,'POST',{uid:GlobalUserid,page:page,pagesize:notifypagesize},function (res) {
        //console.log('系统通知列表',res);
        if (res.code == 1){
            var html = Nldouapp.NotifylistTemplate(res.data);
            if (!append){
                Nldouapp.attachInfiniteScroll('#Notify .infinite-scroll');
                contentempty(res.data.notify.length,'还没有通知~~',$$('#Notify .notifylistwrap'));
            }
            $$('#Notify .notifylistwrap').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Notify .infinite-scroll'));
            $$('#Notify .infinite-scroll-preloader').remove();
        }
        contentempty($$('#Notify .notifylistwrap').find('li').length,'还没有通知~~',$$('#Notify .notifylistwrap'));
        notifyloading = false;
    });
}
/******话题页面******/
function getSubById(url,sid) {
    request(url,'POST',{sid:sid,uid:GlobalUserid},function (res) {
        //console.log('话题页信息',url,res);
        if (res.code == 1){
            var title = '一问奕答话题-'+res.data.name;
            $$('#Subject').data('sharetitle',title);
            var desc = res.data.questions+'个问题，'+res.data.follows+'人关注';
            $$('#Subject').data('sharedesc',desc);

            if ($$('.view-main').data('page')==='Subject'){
                var sid = $$('#Subject').data('id');
                var url = Share_url+'&page=subject'+'&param=sid.'+sid;
                setShare(title,url,desc);
            }


            var subhtml = Nldouapp.SubdetailTemplate(res.data);
            $$('#Subject .sub-header').append(subhtml);
        }
    });
}
function getQuestionBysid(url,page,sid,sort,append)
{
    request(url,'POST',{page:page,sid:sid,sort:sort,pagesize:subjectpagesize},function (res) {
        //console.log('话题页问题列表',url,res);
        if (res.code == 1){
            var html = Nldouapp.QitemTemplate(res.data.question);
            if (!append) {
                $$('#Subject .qlist').find('.qitem').remove();
                $$('#Subject .infinite-scroll').find('.infinite-scroll-preloader').remove();
                var modal = '<div class="infinite-scroll-preloader">'+
                    '<div class="preloader"></div>'+
                    '</div>';
                $$('#Subject .infinite-scroll').append(modal);
                Nldouapp.attachInfiniteScroll('#Subject .infinite-scroll');
                sofa(res.data.question.length,$$("#Subject .qlist"));
            }
            $$('#Subject .qlist').append(html);

        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Subject .infinite-scroll'));
            $$('#Subject .infinite-scroll-preloader').remove();
        }
        subjectloading = false;
        sofa($$('#Subject .qlist .qitem').length,$$("#Subject .qlist"));
    });
}
function followSub(url,sid)
{
    prequest(url,'POST',{sid:sid,uid:GlobalUserid},function (res) {
        fsubstatus = false;
        //console.log('用户关注话题',url,res);
    },function () {
        fsubstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    })
}
function cancelfollowSub(url,sid)
{
    prequest(url,'POST',{sid:sid,uid:GlobalUserid},function (res) {
        fsubstatus = false;
        //console.log('用户取消关注话题',url,res);
    },function () {
        fsubstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
/******问题页面******/
function readQuestion(url,qid){
    request(url,'POST',{qid:qid,uid:GlobalUserid},function (res) {
        //console.log('用户阅读问题',url,res);
    });
}
function getContributer(url,qid)
{
    request(url,'POST',{qid:qid,pagesize:contripagesize},function (res) {
        //console.log('问题页贡献者信息',url,res);
        if (res.code == 1){
            var conhtml = Nldouapp.QContributerTemplate(res.data);
            $$('#Question .contribute-wrap .contributer').html(conhtml);
            $$('#Question .contribute-wrap .contrnum').html(res.data.contrnum);
            $$('#Question .contribute-wrap .beansnum').html(res.data.beansnum);
        }
    });
}
function getQuestionById(qid,url)
{
    request(url,'POST',{qid:qid,uid:GlobalUserid},function (res) {
        //console.log('问题页信息',res);
        if (res.code == 1){
            var title = res.data.question;
            $$('#Question').data('sharetitle',title);
            // var desc = res.data.answers+'个回答，'+res.data.follows+'人关注';
            var desc = '复旦大学社会学教授沈奕斐，为您解答家庭教育的各类问题，陪您共走育儿路';
            $$('#Question').data('sharedesc',desc);

            if($$('.view-main').data('page')==='Question') {
                var qid = $$('#Question').data('id');
                var url = Share_url+'&page=question'+'&param=qid.'+qid;
                setShare(title,url,desc);
            }

            var qhtml = Nldouapp.QdetailTemplate(res.data);
            var qwraphtml = Nldouapp.QdetailwrapTemplate({'link':res.data.anslink,'isresponsed':parseInt(res.data.isresponsed)});
            $$('#Question .qdetail-header').append(qhtml);
            $$('#Question .qdetail-wrap').append(qwraphtml);
            if(res.data.isresponsed==0) getContributer(Getcontributer_url,res.data.id);
        }
    });
}
function getAnswerByQid(qid,page,url,sort,append) {
    request(url,'POST',{qid:qid,page:page,sort:sort,pagesize:questionpagesize},function (res) {
        //console.log('问题答案列表',url,res);
        if (res.code == 1){
            var html = Nldouapp.AnsweritemTemplate(res.data);
            if (!append) {
                $$('#Question .answerlist').find('.aitem').remove();
                $$('#Question .infinite-scroll').find('.infinite-scroll-preloader').remove();
                var modal = '<div class="infinite-scroll-preloader">'+
                    '<div class="preloader"></div>'+
                    '</div>';
                $$('#Question .infinite-scroll').append(modal);
                Nldouapp.attachInfiniteScroll('#Question .infinite-scroll');
                sofa(res.data.answer.length,$$('#Question .answerlist'));
            }
            $$('#Question .answerlist').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Question .infinite-scroll'));
            $$('#Question .infinite-scroll-preloader').remove();
        }
        sofa($$('#Question .answerlist .aitem').length,$$('#Question .answerlist'));
        questionloading = false;
    });
}
function followQuestiion(url,qid)
{
    prequest(url,'POST',{qid:qid,uid:GlobalUserid},function (res) {
        fqstatus = false;
        //console.log('用户关注问题',url,res);
    },function () {
        fqstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function cancelfollowQuestiion(url,qid)
{
    prequest(url,'POST',{qid:qid,uid:GlobalUserid},function (res) {
        fqstatus = false;
        //console.log('用户取消关注问题',url,res);
    },function () {
        fqstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function getBeansrankByqid(url,page,qid)
{
    request(url,'POST',{page:page,qid:qid,pagesize:rankpagesize},function (res) {
        //console.log('问题贡献榜列表',url,res);
        if (res.code == 1){
            var html = Nldouapp.RanklistTemplate(res.data);
            $$('#Beansrank .beansranklist').append(html);
            Nldouapp.detachInfiniteScroll($$('#Beansrank .infinite-scroll'));
            $$('#Beansrank .infinite-scroll-preloader').remove();
        }
    });
}
function getChargeUser(url) {
    request(url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('充值页面',url,res);
        if (res.code == 1){
            var html = Nldouapp.ChargeuserTemplate(res.data);
            $$('#Chargebeans .user-wrap').html(html);
        }
    });
}
function getDechargeUser(url) {
    request(url,'POST',{uid:GlobalUserid},function (res) {
        //console.log('贡献页面',url,res);
        if (res.code == 1){
            var html = Nldouapp.ChargeuserTemplate(res.data);
            $$('#Dechargebeans .user-wrap').html(html);
        }
    });
}
function getQrank(url,page)
{
    $$('#Questionrank .qlistwrap').find('a').remove();
    $$('#Questionrank .infinite-scroll').find('.infinite-scroll-preloader').remove();
    var modal = '<div class="infinite-scroll-preloader">'+
        '<div class="preloader"></div>'+
        '</div>';
    $$('#Questionrank .infinite-scroll').append(modal);
    request(url,'POST',{page:page,pagesize:rankpagesize},function (res) {
        //console.log('女神榜列表',url,res);
        if (res.code == 1){
            $$('#Questionrank .qlistwrap').find('.norankwrap').remove();
            var html = Nldouapp.QranklistTemplate(res.data);
            $$('#Questionrank .qlistwrap').append(html);
            Nldouapp.detachInfiniteScroll($$('#Questionrank .infinite-scroll'));
            $$('#Questionrank .infinite-scroll-preloader').remove();
        }
    });
}
function getQresponsed(url,page,append)
{
    if (!append){
        $$('#Questionrank .qlistwrap').find('a').remove();
        $$('#Questionrank .infinite-scroll').find('.infinite-scroll-preloader').remove();
        var modal = '<div class="infinite-scroll-preloader">'+
            '<div class="preloader"></div>'+
            '</div>';
        $$('#Questionrank .infinite-scroll').append(modal);
    }
    request(url,'POST',{page:page,pagesize:rankpagesize},function (res) {
        //console.log('女神榜已回答列表',res);
        if (res.code == 1){
            $$('#Questionrank .qlistwrap').find('.norankwrap').remove();
            var html = Nldouapp.QreslistTemplate(res.data);
            if (!append){
                Nldouapp.attachInfiniteScroll('#Questionrank .infinite-scroll');
            }
            $$('#Questionrank .qlistwrap').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Questionrank .infinite-scroll'));
            $$('#Questionrank .infinite-scroll-preloader').remove();
        }
        qrankloading = false;
    });
}
function dechargebeans(url,num,qid) {
    num = ("string" === typeof num)?num.replace(/\s/g,""):num.toString();
    if(!(/^[1-9][0-9]*$/).test(num)){
        Nldouapp.alert('请输入正确的数目');
        return false;
    }
    Nldouapp.showIndicator('');
    prequest(url,'POST',{uid:GlobalUserid,num:num,qid:qid},function (res) {
        //console.log('贡献能量豆',url,res);
        if (res.code == 1){
            Nldouapp.hideIndicator();
            Nldouapp.alert('您已成功贡献'+res.data.dechargenum+'个能量豆','');
            if($$('#Question .contrnum').length > 0) $$('#Question .contrnum').html(res.data.contributer);
            if($$('#Question .beansnum').length > 0) $$('#Question .beansnum').html(res.data.qbeans);
        }else{
            Nldouapp.hideIndicator();
            Nldouapp.alert('操作失败<br>您贡献的数目超过了您所拥有的能量豆哦');
        }
        dechargestatus=false;
    },function () {
        Nldouapp.hideIndicator();
        Nldouapp.alert('系统繁忙，请重试','');
        dechargestatus=false;
    });

}
/******提问回答上传状态******/
function startupload()
{
    var html = '<div class="flex-row flex-nowrap flex-justify-center flex-align-center loader-wrap">' +
        '<i class="f7-icons color-green hidden pubsucc" style="margin-right: 15px;width: 25px;height: 25px;">check</i>' +
        '<i class="f7-icons color-red hidden pubfail" style="margin-right: 15px;width: 25px;height: 25px;">close</i>' +
        '<span class="preloader pubproc" style="margin-right: 15px;width: 25px;height: 25px;"></span>' +
        '<span class="pubinfo bold">上传中</span>' +
        '</div>';
    Nldouapp.modal({text:html});
}
function completeupload()
{
    var pubsucc = $$('.loader-wrap .pubsucc');
    var pubinfo = $$('.loader-wrap .pubinfo');
    var pubproc = $$('.loader-wrap .pubproc');
    pubproc.addClass('hidden');
    pubsucc.removeClass('hidden');
    pubinfo.html('上传成功');

    setTimeout(function () {
        Nldouapp.closeModal();
        $$('.popup-edit').remove();
        $$('.popup-qedit').remove();
        $$('.modal').remove();
        $$('.modal-overlay-visible').remove();
    },1000);
}
function failupload()
{
    var pubfail = $$('.loader-wrap .pubfail');
    var pubinfo = $$('.loader-wrap .pubinfo');
    var pubproc = $$('.loader-wrap .pubproc');
    pubproc.addClass('hidden');
    pubfail.removeClass('hidden');
    pubinfo.html('系统繁忙，请重试');

    setTimeout(function () {
        Nldouapp.closeModal('.modal');
        $$('.modal').remove();
        $$('.modal-overlay-visible').remove();
    },2000)
}
function firstupload(content)
{
    var html = '<div class="flex-row flex-nowrap flex-justify-center flex-align-center">' +
        '<i class="f7-icons color-green bold" style="margin-right: 15px;width: 25px;height: 25px;">alarm</i>' +
        '<span class="pubinfo bold">'+content+'</span>' +
        '</div>';
    setTimeout(function () {
        Nldouapp.modal({text:html});
    },1000);
    setTimeout(function () {
        Nldouapp.closeModal('.modal');
        $$('.modal').remove();
        $$('.modal-overlay-visible').remove();
    },3000);
}
/******提问页面******/
function pubQuestion(url) {
    prequest(url,'POST',{},function (res) {
        //console.log('提问',url,res);
        if (res.code == 1){
            var html = Nldouapp.QeditorTemplate({subject:res.data});
            $$('body').append(html);
            Nldouapp.popup('.popup-qedit');
        }
    },function () {
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function checkQuestion(title,subject,desc)
{
    title = title.replace(/\s/g,"");
    var wc = title.length;
    if (wc > 0) {
        if (wc > 50){
            Nldouapp.alert('问题不要超过50个字');
            return false;
        }
    }else{
        Nldouapp.alert('请填写标题');
        return false;
    }
    // if (!(/\?$/).test(title)){
    //     Nldouapp.alert('问题要以问号结尾');
    //     return false;
    // }
    // if (subject === 0){
    //     Nldouapp.alert('请选择话题');
    //     return false;
    // }
    // wc = desc.replace(/\s/g,"").length;
    // if (wc === 0){
    //     Nldouapp.alert('请填写描述');
    //     return false;
    // }
    desc = desc.replace(/\n|\r\n|\r/g,"<br>");
    return {title:title,subject:subject,desc:desc};
}
function addQuestion(url,subid,question,desc)
{
    if (questionuploading === false){
        questionuploading = true;
        startupload();
        prequest(url,'POST',{uid:GlobalUserid,subid:subid,question:question,description:desc},function (res) {
            //console.log('用户添加问题',url,res);
            if (res.code == 1){
                completeupload();
                if(res.data.firstq){
                    firstupload('首次发布问题,获得100能量豆');
                }
                questionuploading=false;
            }else{
                failupload();
                questionuploading=false;
            }
        },function(){
            //console.log('ajax error');
            failupload();
            questionuploading=false;
        });
    }
}
/******回答页面******/
function checkAnswerExist(url,qid,uid)
{
    request(url,'POST',{qid:qid,uid:uid},function (res) {
        //console.log('判断用户是否回答过该问题',url,res);
        var ret = {};
        if (res.code == 0){
            ret = {type:'addAnswer',answer:'',aid:''};
        }else{
            var answer = res.data.answer.replace(/<br>|<\/p>/g,"\n").replace(/<p>/g,'');
            ret = {type:'updateAnswer',answer:answer,aid:res.data.aid};
        }
        var html = Nldouapp.EditorTemplate({'id':qid,'cid':ret.aid,'type':ret.type,'title':'添加观点','suggest':'填写观点内容','content':ret.answer});
        $$('body').append(html);
        Nldouapp.popup('.popup-edit');
    });
}
function checkAnswer(answer)
{
    var wc = answer.replace(/\s/g,"").length;
    if (wc == 0) {
        Nldouapp.alert('请填写答案');
        return false;
    }
    answer = answer.replace(/\n|\r\n|\r/g,'<br>');
    return answer;
}
function addAnswer(url,qid,uid,answer)
{
    if (answeruploading === false){
        answeruploading = true;
        startupload();
        prequest(url,'POST',{qid:qid,uid:uid,answer:answer},function (res) {
            //console.log('用户添加回答',url,res);
            if (res.code == 1){
                completeupload();
                if(res.data.firstans){
                    firstupload('首次发布观点,获得100能量豆');
                }
                answeruploading=false;
                var qid = $$('#Question').data('id');
                mainView.router.reloadPage('question.html?qid='+qid);
            }else{
                failupload();
                answeruploading=false;
            }
        },function(){
            //console.log('ajax error');
            failupload();
            answeruploading=false;
        })
    }
}
function updateAnswer(url,aid,answer)
{
    if (answeruploading === false){
        answeruploading = true;
        startupload();
        prequest(url,'POST',{aid:aid,answer:answer},function (res) {
            //console.log('用户修改回答',url,res);
            if (res.code == 1){
                completeupload();
                answeruploading=false;
                var qid = $$('#Question').data('id');
                mainView.router.reloadPage('question.html?qid='+qid);
            }else{
                failupload();
                answeruploading=false;
            }
        },function(){
            //console.log('ajax error');
            failupload();
            answeruploading=false;
        });
    }

}
/******答案页面******/
function getAnswerById(aid,url)
{
    request(url,'POST',{aid:aid,uid:GlobalUserid},function (res) {
        //console.log('答案页信息',url,res);
        if (res.code=1){
            var title = res.data.question+'-'+res.data.userName+'的回答';
            $$('#Answer').data('sharetitle',title);
            var desc = res.data.answer.replace(/<.*>/g,"").substr(0,30)+'...';
            $$('#Answer').data('sharedesc',desc);

            if ($$('.view-main').data('page')==='Answer'){
                var aid = $$('#Answer').data('id');
                var url = Share_url+'&page=answer'+'&param=aid.'+aid;
                setShare(title,url,desc);
            }

            var navhtml = Nldouapp.AdetailnavTemplate(res.data);
            var chtml = Nldouapp.AdetailcontentTemplate(res.data);
            var tbhtml = Nldouapp.AdetailtoolbarTemplate(res.data);
            $$('.answernav').html(navhtml);
            $$('#Answer').append(tbhtml);
            $$('#Answer .page-content .answer-content').html(chtml);
        }
    });
}
function getCommentsByAid(aid,page,url,sort,append)
{
    request(url,'POST',{aid:aid,page:page,sort:sort,pagesize:answerpagesize,uid:GlobalUserid},function (res) {
        //console.log('答案评论列表',url,res);
        if (res.code == 1){
            var html = Nldouapp.CommentitemTemplate(res.data);
            if (!append) {
                $$('#Answer .commentlist').find('.citem').remove();
                $$('#Answer .infinite-scroll').find('.infinite-scroll-preloader').remove();
                var modal = '<div class="infinite-scroll-preloader">'+
                    '<div class="preloader"></div>'+
                    '</div>';
                $$('#Answer .infinite-scroll').append(modal);
                Nldouapp.attachInfiniteScroll('#Answer .infinite-scroll');
                sofa(res.data.comment.length,$$('#Answer .commentlist'));
            }
            $$('#Answer .commentlist').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Answer .infinite-scroll'));
            $$('#Answer .infinite-scroll-preloader').remove();
        }
        sofa($$('#Answer .commentlist .citem').length,$$('#Answer .commentlist'));
        answerloading = false;
    });
}
function agreeAnswer(aid,url)
{
    prequest(url,'POST',{aid:aid,uid:GlobalUserid},function (res) {
        agreestatus = false;
        //console.log('用户赞同答案',url,res);
    },function () {
        agreestatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    })
}
function cancelagreeAnswer(aid,url)
{
    prequest(url,'POST',{aid:aid,uid:GlobalUserid},function (res) {
        agreestatus = false;
        //console.log('用户取消答案赞同',url,res);
    },function () {
        agreestatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function collectAnswer(aid,url)
{
    prequest(url,'POST',{aid:aid,uid:GlobalUserid},function (res) {
        collstatus = false;
        //console.log('用户收藏答案',url,res);
    },function () {
        collstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function cancelcollectAnswer(aid,url)
{
    prequest(url,'POST',{aid:aid,uid:GlobalUserid},function (res) {
        //console.log('用户取消答案收藏',url,res);
        collstatus = false;
    },function () {
        collstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
/******发布答案评论页面******/
function checkComment(comment)
{
    var wc = parseInt(comment.replace(/\s/g,"").length);
    if (wc === 0) {
        Nldouapp.alert('请输入评论内容');
        return false;
    }
    comment = comment.replace(/\n|\r\n|\r/g,'<br>');
    return comment;
}
function addComment(url,aid,uid,comment)
{
    if (commentuploading === false){
        commentuploading = true;
        startupload();
        prequest(url,'POST',{aid:aid,uid:uid,comment:comment},function (res) {
            //console.log('用户添加评论',url,res);
            if (res.code == 1){
                completeupload();
                commentuploading = false;
                var aid = $$('#Answer').data('id');
                mainView.router.reloadPage('answer.html?aid='+aid);
            }else{
                failupload();
                commentuploading = false;
            }
        },function() {
            //console.log('ajax error');
            failupload();
            commentuploading = false;
        });
    }
}
function thumbupComment(cid,url)
{
    prequest(url,'POST',{cid:cid,uid:GlobalUserid},function (res) {
        thumbupstatus = false;
        //console.log('用户点赞评论',url,res);
    },function () {
        thumbupstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
function cancelthumbupComment(cid,url)
{
    prequest(url,'POST',{cid:cid,uid:GlobalUserid},function (res) {
        thumbupstatus = false;
        //console.log('用户取消点赞评论',url,res);
    },function () {
        thumbupstatus = false;
        Nldouapp.alert('系统繁忙，请重试','');
    });
}
/******发布评论回复页面******/
function checkResponse(comment)
{
    var wc = parseInt(comment.replace(/\s/g,"").length);
    if (wc === 0) {
        Nldouapp.alert('请输入回复内容');
        return false;
    }
    comment = comment.replace(/\n|\r\n|\r/g,'<br>');
    return comment;
}
function responseComment(url,cid,uid,comment)
{
    if (responseuploading === false){
        responseuploading = true;
        startupload();
        prequest(url,'POST',{cid:cid,uid:uid,comment:comment},function (res) {
            //console.log('用户添加回复',url,res);
            if (res.code == 1){
                completeupload();
                responseuploading = false;
                var aid = $$('#Answer').data('id');
                mainView.router.reloadPage('answer.html?aid='+aid);
            }else{
                failupload();
                responseuploading = false;
            }
        },function() {
            //console.log('ajax error');
            failupload();
            responseuploading = false;
        })
    }
}
/******评论回复页面******/
function getResponseByCid(cid,page,url)
{
    request(url,'POST',{cid:cid,page:page,pagesize:responsepagesize},function (res) {
        //console.log('评论回复列表',url,res);
        if (res.code == 1){
            var html = Nldouapp.ResponseitemTemplate(res.data.response);
            $$('#Response .response-wrap ul').append(html);
        }
        if (res.data.end == 1){
            Nldouapp.detachInfiniteScroll($$('#Response .infinite-scroll'));
            $$('#Response .infinite-scroll-preloader').remove();
        }
        responseloading = false;
    });
}